package com.xiaoyu.lanling.feature.voicematch

import `in`.srain.cube.util.LocalDisplay
import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.model.User
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.activity.base.BaseBottomSheetDialogFragment
import com.xiaoyu.lanling.common.Constant
import com.xiaoyu.lanling.event.voicematch.VoiceMatchContinueMatchEvent
import com.xiaoyu.lanling.feature.voicematch.data.VoiceMatchData
import com.xiaoyu.lanling.feature.voicematch.datamodel.EvaluateEnum
import com.xiaoyu.lanling.feature.voicematch.log.VoiceMatchLogger
import com.xiaoyu.lanling.media.image.ImageViewUtil
import kotlinx.android.synthetic.main.voice_match_evaluate_dialog.*

class VoiceMatchEvaluateDialogFragment : BaseBottomSheetDialogFragment() {

    companion object {
        private val TAG = VoiceMatchEvaluateDialogFragment::class.java.simpleName

        fun show(fragmentManager: FragmentManager, user: User, destroyListener: OnDestroyListener) {
            val arguments = Bundle()
            arguments.putSerializable(Constant.IntentKey.KEY_USER, user)

            val dialogFragment = VoiceMatchEvaluateDialogFragment()
            dialogFragment.arguments = arguments
            dialogFragment.isCancelable = false
            dialogFragment.setOnDestroyListener(destroyListener)
            dialogFragment.show(fragmentManager, TAG)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogFullScreenDimStyle)
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        dialog.setOnShowListener { dialogInterface ->
            (dialogInterface as? BottomSheetDialog)?.let {
                setupFullHeight(it)
            }
        }
        return dialog
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = FrameLayout(AppContext.getContext())
        val layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        view.layoutParams = layoutParams
        return view
    }

    override fun onViewCreatedSafelyAfterAppFinishInit(view: View, savedInstanceState: Bundle?) {
        (view as? ViewGroup)?.let {
            layoutInflater.inflate(R.layout.voice_match_evaluate_dialog, it, true)
        }
        initView()
        initBind()
        VoiceMatchLogger.evaluateDialogShow()
    }

    private fun initView() {
        val user = (arguments?.getSerializable(Constant.IntentKey.KEY_USER) as? User) ?: return
        ImageViewUtil.loadAvatarRound(avatar, user, 96)
        name?.setUser(user)

        dislike?.setViewTag(user)
        like?.setViewTag(user)
    }

    private fun initBind() {
        close?.setOnClickDebounceListener {
            VoiceMatchLogger.evaluateDialogCloseClick()
            dismiss()
        }
        dislike?.setOnClickDebounceListener {
            val user = it.getViewTag<User>() ?: return@setOnClickDebounceListener
            VoiceMatchData.matchEvaluate(Any(), user.uid, EvaluateEnum.BAD)
            VoiceMatchLogger.evaluateRequestStart(EvaluateEnum.BAD.type)
            like?.visibility = View.GONE
        }
        like?.setOnClickDebounceListener {
            val user = it.getViewTag<User>() ?: return@setOnClickDebounceListener
            VoiceMatchData.matchEvaluate(Any(), user.uid, EvaluateEnum.GOOD)
            VoiceMatchLogger.evaluateRequestStart(EvaluateEnum.GOOD.type)
            dislike?.visibility = View.GONE
        }
        continue_match_button?.setOnClickDebounceListener {
            VoiceMatchContinueMatchEvent().post()
            dismiss()
        }
        give_up_button?.setOnClickDebounceListener {
            VoiceMatchLogger.evaluateDialogCloseClick()
            dismiss()
        }
    }

    private fun setupFullHeight(bottomSheetDialog: BottomSheetDialog) {
        bottomSheetDialog.findViewById<View>(R.id.design_bottom_sheet)?.let {
            val behavior = BottomSheetBehavior.from(it)
            it.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
            it.layoutParams = it.layoutParams
            behavior.peekHeight = LocalDisplay.SCREEN_HEIGHT_PIXELS
            behavior.state = BottomSheetBehavior.STATE_EXPANDED
            behavior.isHideable = true
        }
    }
}

